
package org.itboys.mobile.service.mongo.mobile;

import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.entity.mongo.SundayPermission;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_关联_权限。
 *
 */
@Service
public class SundayMobilePermissionService extends BaseMongoService<SundayPermission> {


	@Resource(name="mobileDs")
	private MongoDataSource sundayDataSource;
	@Override
	protected MongoDataSource getMongoDataSource() {
		return sundayDataSource;
	}
	@Override
	protected Class<SundayPermission> getEntityClass() {
		return SundayPermission.class;
	}
	//


	/**
	 * 变更门店库存
	 * @param productIds
	 * @param nums
	 * @param shopId
	 */
	public void changeStore(List<Long> productIds,List<Integer> nums,Long shopId){
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("memberId",shopId);
		param.put("productId in",productIds);
		param.put("isDeleted",0);
		List<SundayPermission> permissions=super.list(param);
		for(SundayPermission permission:permissions){
			for(int i =0;i<productIds.size();i++){
				Long productId =productIds.get(i);
				if(LongUtil.isNotZreo(permission.getProductId())&&
						permission.getProductId().longValue()==productId.longValue()){
					permission.setStore(permission.getStore()-nums.get(i));
					break;
				}
			}
		}
		if(ListUtil.isNotNull(permissions)){
			super.batchUpdateWithoutLogin(permissions);
		}
	}
/*


	*//**、
	 * 通过用户ID和类型获取权限（含库存和价格）
	 * @param shopId
	 * @param type
	 * @return
	 *//*
	public void getPermission(Long shopId,Integer type,List<SundayProduct> products){
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("memberId",shopId);
		param.put("type",type);
		param.put("isDeleted",0);
		if(ListUtil.isNotNull(products)){
			List<Long> productIds = new ArrayList<Long>();
			for(SundayProduct p:products){
				productIds.add(p.getId());
			}
			param.put("otherId in",productIds);
		}
		List<SundayPermission> permissions=super.list(param);
		//如果没有设置库存，执行0库存和标准商品价格
		if(ListUtil.isNotNull(products)){
			for(int i=0;i<products.size();i++ ){
				SundayProduct product=products.get(0);
				product.setStore(0);
				boolean isZeroStore=true;
				for(SundayPermission permission:permissions){
					if(LongUtil.isNotZreo(permission.getOtherId())&&
							permission.getOtherId().longValue()==product.getId()){
						if(permission.getStore()!=null&&permission.getStore()>0l){
							isZeroStore=false;
							product.setStore(permission.getStore());
							if(product.getPrice()!=null){
								product.setPrice(product.getPrice());
							}
						}


					}
				}
				if(isZeroStore){
					//删除0库存商品
					products.remove(i);
					continue;
				}
			}
		}


		return ;
	}
	*//**
	 * 扣减库存
	 *//*
	*//**
	 *
	 * @param memberId
	 * @param otherIds
	 * @param stores
	 *//*
	public void changeStore(Long memberId,List<Long> otherIds,List<Integer> stores){
		if(ListUtil.isNull(otherIds)||ListUtil.isNull(stores)||
				otherIds.size()!=stores.size())return;
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("memberId",memberId);
		param.put("type",1);
		param.put("isDeleted",0);
		param.put("otherId in",otherIds);
		List<SundayPermission> permissions=super.list(param);
		for(int i=0;i<otherIds.size();i++){
			Integer store = stores.get(i);
			Long otherId=otherIds.get(i);
			for(SundayPermission permission:permissions){
				if(LongUtil.isNotZreo(permission.getOtherId())&&
						otherId.longValue()==permission.getOtherId().longValue()&&
						permission.getStore()!=null
						){
						super.update(permission.getId(),"store",permission.getStore()-store);

				}
			}
		}

	}*/







}

